package JdbcTest.JdbcUtil;

import java.sql.*;
import java.util.Properties;
import java.util.ResourceBundle;

public class JdbcUtil {
    private static Connection conn;
    private static PreparedStatement pst;
    private static ResultSet rs;
    static{
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static Connection MyConnection(){
        ResourceBundle bundle = ResourceBundle.getBundle("DatabaseFile");
        try {
            conn = DriverManager.getConnection(bundle.getString("url"),bundle.getString("username"),bundle.getString("password"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    //查询表中全部数据
    public static ResultSet select(Connection conn){
        try {
            String sql = "select * from t_student";
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }

    public static void MyClose(Connection conn,PreparedStatement pst,ResultSet rs){
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (pst != null) {
            try {
                pst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void MyClose(Connection conn,PreparedStatement pst){
        if (pst != null) {
            try {
                pst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void MyClose(Connection conn,ResultSet rs){
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
